package com.malou.client.ventas.variacion;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class VariacionDialog extends Dialog {

	protected HiddenField<String> id;
	protected TextField<String> descripcion;
	protected Button guardar;
	protected Button cancelar;
	protected Status status;
	private FormPanel panel;

	private FormBinding formBindings;
	
	private MalouConstants constants = GWT.create(MalouConstants.class);

	public VariacionDialog() {
		super();
		setButtonAlign(HorizontalAlignment.LEFT);
		setButtons("");
		setModal(true);
		setSize(300, 50);

		panel = new FormPanel();
		panel.setBorders(false);
		panel.setBodyBorder(false);
		panel.setPadding(5);
		panel.setHeaderVisible(false);
		panel.setLabelWidth(70);

		formBindings = new FormBinding(panel, false);
		addFields();

		setFocusWidget(descripcion);
		Component comp[] = { descripcion, guardar };
		Utilities.Enter(comp);
	}

	@Override
	protected void createButtons() {
		super.createButtons();

		if (constants == null) {
			constants = GWT.create(MalouConstants.class);
		}

		status = new Status();
		status.setBusy(constants.guardando());
		status.hide();
		status.setAutoWidth(true);
		getButtonBar().add(status);

		getButtonBar().add(new FillToolItem());

		cancelar = new Button(constants.cancelar(),
				IconHelper.createPath("images/icons/cancel.png"));
		cancelar.setSize(70, 25);
		cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				VariacionDialog.this.hide();
			}

		});

		guardar = new Button(constants.guardar(),
				IconHelper.createPath("images/icons/save_all.png"));
		guardar.setSize(70, 25);

		guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				if (validate()) {
					status.show();
					getButtonBar().disable();

					Variacion  tc= new Variacion();

					if (id.getValue() != null)
						tc.setId(Integer.valueOf(id.getValue()));
					tc.setDescripcion(descripcion.getValue());

					Variaciones.variacionDialog = VariacionDialog.this;
					Variaciones.save(tc);
				}

			}
		});

		addButton(guardar);
		addButton(cancelar);

	}

	protected boolean validate() {
		boolean nom = descripcion.isValid();
		return nom;
	}

	public void reset() {
		setIconStyle("icon-new");
		setHeading("Nuevo modelo");

		id.clear();
		descripcion.clear();
		status.hide();
		getButtonBar().enable();
	}

	public void setStore(ListStore<BeanModel> store) {
		formBindings.addFieldBinding(new FieldBinding(id, "id"));
		formBindings.addFieldBinding(new FieldBinding(descripcion,
				"descripcion"));
		formBindings.setStore(store);
	}

	private void addFields() {

		id = new HiddenField<String>();
		id.setVisible(false);
		panel.add(id);

		descripcion = new TextField<String>();
		descripcion.setFieldLabel(constants.gastoDescripcion());
		descripcion.setAllowBlank(false);
		panel.add(descripcion, new FormData("95%"));

		add(panel);

	}

	public void beforeEdit() {
		setIconStyle("icon-update");
		setHeading("Modificar modelo");

		status.hide();
		getButtonBar().enable();
	}

	public void bind(BeanModel selectedModel) {
		formBindings.bind(selectedModel);
	}

	public void unbind() {
		formBindings.unbind();
	}
}
